package com.xingxuecn.limit.web.action.sys;

import com.opensymphony.xwork2.ActionSupport;
import com.xingxuecn.limit.common.model.Menu;
import com.xingxuecn.limit.common.model.Page;
import com.xingxuecn.limit.common.model.ResultModel;
import com.xingxuecn.limit.common.utils.ResponseUtils;
import com.xingxuecn.limit.service.MenuService;
import com.xingxuecn.limit.service.impl.MenuServiceImpl;

import org.apache.struts2.ServletActionContext;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Kris at 2018/9/6 11:20
 */
public class MenuAction extends ActionSupport {

    private MenuService menuService = new MenuServiceImpl();

    private Integer page;
    private Integer rows;
    private Menu menu = new Menu();


    public String index() {
        return SUCCESS;
    }

    public String loadAll() throws SQLException {
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(menuService.findAll(), response);
        return null;
    }

    public String search() {
        HttpServletResponse response = ServletActionContext.getResponse();
        List<Menu> list = null;
        try {
            list = menuService.search(menu);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResponseUtils.writeJson(list, response);
        return null;
    }

    public String searchPage() {
        HttpServletResponse response = ServletActionContext.getResponse();
        Page result = null;
        try {
            result = menuService.searchPage(menu,page,rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResponseUtils.writeJson(result, response);
        return null;
    }


    public String add() {
        ResultModel resultModel = null;
        try {
            menuService.add(menu);
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "新增失败");
        }
        resultModel = new ResultModel(201, "新增成功");
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }

    public String update() {
        ResultModel resultModel = null;
        try {
            menuService.update(menu);
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "修改失败");
        }
        resultModel = new ResultModel(201, "修改成功");
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }


    public String delete() {
        ResultModel resultModel = null;
        try {
            menuService.delete(menu.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            resultModel = new ResultModel(500, "删除失败");
        }
        resultModel = new ResultModel(200, "删除成功");
        HttpServletResponse response = ServletActionContext.getResponse();
        ResponseUtils.writeJson(resultModel, response);
        return null;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }
}
